package com.wyp.controller.admin;

import com.wyp.pojo.Admins;
import com.wyp.service.impl.AdminsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AuthorityManController {

    @Autowired
    private AdminsServiceImpl adminsService;

    @RequestMapping("/manager_manageinfo.html")
    public String tologin(Model model) {
        List<Admins> adminsList = adminsService.qureyAllAdmins();
        model.addAttribute("alladmins", adminsList);
        return "admin/manager/manager_manageinfo";
    }

    @GetMapping(value = "/authority_manage.html/{usernumber}")
    public String tomanupdate(
            @PathVariable("usernumber") String usernumber,
            Model model
    ) {
        Admins admins = adminsService.findByAdminaccount(usernumber);
        model.addAttribute("updateadmin", admins);
        return "admin/manager/authority_manage";
    }

    @PostMapping("/authority_manage.html/{adminaccount}")
    public String manupdate(
            @PathVariable("adminaccount") String adminaccount,
            @Nullable @RequestParam("auth") String[] auth,
            Model model
    ) {
        Admins admins = adminsService.findByAdminaccount(adminaccount);
        String[] check = {"", "", ""};
        if (auth != null) {
            for (String a : auth) {
                if (a.equals("1")) {
                    check[0] = "1";
                }
                if (a.equals("2")) {
                    check[1] = "1";
                }
                if (a.equals("3")) {
                    check[2] = "1";
                }
            }
        } else {
            admins.setChangestuauth(false);
            admins.setChangetestauth(false);
            admins.setChangeadminauth(false);
        }
        if (check[0].equals("")) {
            admins.setChangestuauth(false);
        } else {
            admins.setChangestuauth(true);
        }
        if (check[1].equals("")) {
            admins.setChangetestauth(false);
        } else {
            admins.setChangetestauth(true);
        }
        if (check[2].equals("")) {
            admins.setChangeadminauth(false);
        } else {
            admins.setChangeadminauth(true);
        }
        boolean authman = adminsService.updateAdmin(admins);
        return "redirect:/admin/manager_manageinfo.html";
    }

}
